package com.ht.aems.fesmodel;

import com.ht.aems.communicate.IPortInfo;
import com.ht.aems.communicate.ITransmitPort;
import com.ht.aems.communicate.PortListener;
import com.ht.aems.communicate.UdpTransmitPort;
import com.ht.aems.communicate.UdpPortListener;
import org.apache.log4j.Logger;

import java.io.Serializable;
import java.net.DatagramPacket;

/**
 * class CommUnit
 * @author Dong Shufeng
 * Date: 2006-3-10
 * @version 1.0
 */
public class CommUnit extends AbstractUnit implements PortListener, UdpPortListener, Serializable {
    private static Logger log = Logger.getLogger(CommUnit.class);

    private IPortInfo portInfo;
    private int analogNum;
    private int digitalPointNum;
    private int digitalBitNum;
    private int electricNum;
    private int rtuType =-1;
    transient private ITransmitPort port;
    transient private ProtocolHandler handler;
    private String protocolName;

    public CommUnit() {
        this(-1, "");
    }

    public CommUnit(String name) {
        this(-1, name);
    }

    public CommUnit(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getAnalogNum() {
        return analogNum;
    }

    public void setAnalogNum(int analogNum) {
        this.analogNum = analogNum;
    }

    public int getDigitalPointNum() {
        return digitalPointNum;
    }

    public void setDigitalPointNum(int digitalPointNum) {
        this.digitalPointNum = digitalPointNum;
    }

    public int getDigitalBitNum() {
        return digitalBitNum;
    }

    public void setDigitalBitNum(int digitalBitNum) {
        this.digitalBitNum = digitalBitNum;
    }

    public int getElectricNum() {
        return electricNum;
    }

    public void setElectricNum(int electricNum) {
        this.electricNum = electricNum;
    }

    public IPortInfo getPortInfo() {
        return portInfo;
    }

    public void setPortInfo(IPortInfo portInfo) {
        this.portInfo = portInfo;
    }

    public ITransmitPort getPort() {
        return port;
    }

    public void setPort(ITransmitPort port) {
        if(this.port != null)
            this.port.removePortListener(this);
        this.port = port;
        if(port != null && port instanceof UdpTransmitPort)
            ((UdpTransmitPort)port).addUdpPortListener(this);
        else if(port != null)
            port.addPortListener(this);
    }

    public ProtocolHandler getHandler() {
        return handler;
    }

    public void setHandler(ProtocolHandler handler) {
        this.handler = handler;
    }

    public String toString() {
        return name;
    }

    public void dataUploaded(byte[] data) {
        if(handler != null) {
            handler.setCommUnitId(this.getId());
            try {
                handler.handle(data);
            } catch(Exception e) {
                log.warn(e);
                e.printStackTrace();
            }
        }
    }

    public void dataSent(byte[] data) {
    }

    public String getProtocolName() {
        return protocolName;
    }

    public void setProtocolName(String handlerName) {
        this.protocolName = handlerName;
    }


    public int getRtuType() {
        return rtuType;
    }

    public void setRtuType(int rtuType) {
        this.rtuType = rtuType;
    }

    /** @noinspection CloneDoesntCallSuperClone*/
    public Object clone() throws CloneNotSupportedException {
        CommUnit r = new CommUnit(this.getId(), this.getName());
        r.setAnalogNum(this.getAnalogNum());
        r.setDigitalPointNum(this.getDigitalPointNum());
        r.setDigitalBitNum(this.getDigitalBitNum());
        r.setElectricNum(this.getElectricNum());
        r.setPortInfo(this.getPortInfo());
        r.setHandler(this.getHandler());
        r.setProtocolName(this.getProtocolName());
        r.setRtuType(this.getRtuType());
        //r.setPort(this.getPort());
        r.setParentUnitId(this.getParentUnitId());
        return r;
    }

    public boolean isSet() {
        return portInfo != null;
    }

    public void paste(CommUnit source) {
        this.setName(source.getName());
        this.setAnalogNum(source.getAnalogNum());
        this.setDigitalPointNum(source.getDigitalPointNum());
        this.setDigitalBitNum(source.getDigitalBitNum());
        this.setElectricNum(source.getElectricNum());
        this.setPortInfo(source.getPortInfo());
        this.setHandler(source.getHandler());
        this.setProtocolName(source.getProtocolName());
        this.setRtuType(source.getRtuType());
        //r.setPort(this.getPort());
        this.setParentUnitId(source.getParentUnitId());
    }

    public void dataUploaded(DatagramPacket packet) {
        if(handler != null) {
            handler.setCommUnitId(this.getId());
            try {
                handler.handle(packet);
            } catch(Exception e) {
                log.warn(e);
                e.printStackTrace();
            }
        }
    }

    public void dataSent(DatagramPacket packet) {
    }
}
